package cn.zsc.netdisk.mapper;

import cn.zsc.netdisk.pojo.Advise;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

/**
 * @author ccying
 * @date 2021/3/7
 */
@Mapper
@Component
public interface AdviseMapper {
    /**
     * 根据用户账号查找所有通知
     * @param uid 用户账号
     * @param offset 分页起始坐标
     * @param row 查询条数
     * @return 通知信息数组
     * */
    @Select("select * from t_advise where uid=#{uid} order by created desc limit #{offset}, #{row}")
    Advise[] findAllByUid(String uid, int offset, int row);

    /**
     * 一键已读通知
     * @throws  Exception 执行SQL语句可能抛出异常
     * */
    @Update("update t_advise set is_read=1")
    void isReadAll() throws Exception;

    /**
     * 已读通知
     * @param id 通知id
     * @throws Exception 执行SQL语句可能抛出异常
     * */
    @Update("update t_advise set is_read=1 where id=#{id}")
    void isRead(int id) throws Exception;

    /**
     * 一键清除通知
     * @throws Exception 执行SQL语句可能抛出异常
     * */
    @Delete("delete from t_advise")
    void deleteAll() throws Exception;

    /**
     * 添加通知记录
     * @param advise 通知信息
     * @throws Exception 执行SQL语句可能抛出异常
     * */
    @Insert("Insert into t_advise(uid,title,content,created,is_read) " +
            "value(#{advise.uid},#{advise.title},#{advise.content},now(),#{advise.isRead})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void add(@Param("advise") Advise advise) throws Exception;
}
